//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_jail.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_params","_caller_name","_caller_uid","_player_uid","_player_name","_player_info","_action","_authorized","_player","_player_id","_counter","_player_pos","_new_pos"];
PARAMS_3(_params,_caller_name,_caller_uid);

_authorized = _params select 0;
_action = _params select 1;
_player = _params select 2;
_player_uid = _params select 3;
_player_name = _params select 4;

if (isServer) then {
	if (!isnil "mp_rights_fnc_isMember" && {_player call mp_rights_fnc_isMember}) exitWith {ERROR_1("You cannot punish a member",_this);};
	_player_info = [];
	_index = GVAR(PlayerIndex) find _player_uid;
	if (_index >= 0) then {
		_player_info = GVAR(PlayerInfo) select _index;
	};
	TLOG_1("JAIL",_player_info);
	if (count _player_info > 0) then {
		// _player_info set [0, _name];	// Player name at login
		// _player_info set [1, _uid];		// Player UID
		// _player_info set [2, _id];		// Player DirectPlay ID at login
		// _player_info set [3, MSO_AA_PLAYER_ID_COUNT]; // Player ID at login
		// _player_info set [4, time];		// Player first login time
		// _player_info set [5, getPos player];// Initial Position
		// _player_info set [6, time];		// Player last login time
		// _player_info set [7, 0];		// Player jails
		// _player_info set [8, 0];		// Player kicks
		// _player_info set [9, 0];		// Player bans
		// _player_info set [10, 0];		// Player teleports
		// _player_info set [11, 0];		// Player vehicle crashes
		
		_player_id = (_player_info select 3);
		_player_name = (_player_info select 0);
		_player_pos = (_player_info select 5);
		
		// Increment the counter of the actions performed against the user
		_counter = (_player_info select 7) + 1;
		_player_info set [7, _counter];
		GVAR(PlayerInfo) set [_index, _player_info];
		
		if (count _player_pos > 0 && ((_player_pos vectorDistance [0,0,0]) < 1)) then { _player_pos = []; };
		if (count _player_pos <= 0 && {!isnil "D_FLAG_BASE"}) then { _player_pos = position D_FLAG_BASE; };
		if (count _player_pos <= 0 && {markerType "base_respawn" != ""}) then { _player_pos = markerPos "base_respawn"; };
		if (count _player_pos <= 0 && {markerType "respawn_west" != ""}) then { _player_pos = markerPos "respawn_west"; };
		
		_player = objNull;
		{ if (getPlayerUID _x == _player_uid) exitWith { _player = _x; };
		} foreach playableUnits;
		
		if (!isNull _player) then {
			_new_pos = [0,0,1];
			if (markerType "MSO_PUNISHMENT" != "") then { _new_pos = markerPos "MSO_PUNISHMENT"; };
			[_player,_new_pos,_player_pos] spawn {
				waituntil {(_this select 0) setPos (_this select 1); sleep 0.321; (!(OGETVAR((_this select 0),QGVAR(IS_PLAYER_JAILED),false)) || (!alive (_this select 0))) };
				if (alive (_this select 0)) then {
					(_this select 0) setPos (_this select 2);
				};
			};
		} else {
			ERROR_1("Player Not Found",_player_uid);
		};
	} else {
		ERROR_1("Missing Player Info",_player_uid);
	};
};
if (!isDedicated) then {
	//if (!isnil "mp_rights_fnc_isMember" && {_player call mp_rights_fnc_isMember}) exitWith {};
	if (_player_uid == getPlayerUID player) then {
		if (!isnil "mp_rights_fnc_isMember" && {player call mp_rights_fnc_isMember}) exitWith {};
		0 spawn {
			private ["_dec"];
			_dec = 60;
			PSETVARP(QGVAR(IS_PLAYER_JAILED),true);
			disableUserInput true;
			waituntil {
				titleText [Format[localize "STR_MSO_AA_PUNISHED",_dec], "BLACK FADED"];
				sleep 1;
				_dec = _dec - 1;
				(!(PGETVAR(QGVAR(IS_PLAYER_JAILED),false)) || _dec <= 0 || (!alive player))
			};
			PSETVARP(QGVAR(IS_PLAYER_JAILED),false);
			titleText [localize "STR_MSO_AA_RELEASED", "BLACK FADED",5];
			titleFadeOut 5;
			disableUserInput false;
		};
	} else {
		hint format[localize "STR_MSO_AA_JAILED", _player_name, _player_uid];
	};
};

TRACE("End");
TMFPROFILERSTOP;
